/*
 * @Description:
 * @Author:
 * @Date: 2024-10-17 23:10:20
 * @LastEditTime: 2025-02-18 22:32:28
 * @LastEditors: XXX
 */
import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
import Layout from '../layout/index.vue'

interface FileModule { default: Array<RouteRecordRaw> }

const constantFiles = import.meta.glob('./constantMoudles/*.ts', { eager: true }) as Record<string, FileModule>
let constantModules: Array<RouteRecordRaw> = []

for (const path in constantFiles) {
  const module: FileModule = (await constantFiles[path])
  constantModules = constantModules.concat(module.default)
}

const permissionFiles = import.meta.glob('./permissionModules/*.ts', { eager: true }) as Record<string, FileModule>
let permissionModules: Array<RouteRecordRaw> = []

for (const path in permissionFiles) {
  const module: FileModule = await permissionFiles[path]
  permissionModules = permissionModules.concat(module.default)
}

export const constantRoutes: Array<RouteRecordRaw> = [
  {
    path: '/redirect',
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import(/* webpackChunkName: "redirect" */ '@/views/redirect/index.vue')
      }
    ]
  }, {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import(/* webpackChunkName: "dashboard" */'@/views/dashboard/index.vue'),
        name: 'Dashboard',
        meta: {
          title: 'dashboard',
          icon: 'dashboard',
          affix: true
        }
      }
    ]
  },
  ...constantModules
]

console.log('constantRoutes :>> ', constantRoutes);

export const asyncRoutes: Array<RouteRecordRaw> = [
  ...permissionModules
]

console.log('asyncRoutes AAA:>> ', asyncRoutes);

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: constantRoutes
}) as any

export function resetRouter () {
  const newRouter = router as any
  router.matcher = newRouter.matcher
}

export default router
